package lolxxs.chenyanting.interceptor;

import lolxxs.chenyanting.annotation.Cacheable;
import lolxxs.chenyanting.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Component
public class CacheInterceptor implements HandlerInterceptor {

    @Autowired
    private Cache cache;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
//        HandlerMethod method = (HandlerMethod) handler;
//        Cacheable cacheable = method.getMethodAnnotation(Cacheable.class);
//        if (cacheable != null) {
//            cache.get();
//            System.out.println(request.getRequestURI());
//            ServletInputStream inputStream = request.getInputStream();
//            byte[] buffer = new byte[1024];
//            inputStream.read(buffer);
//            System.out.println(new String(buffer));

//            return false;
//            cache.get()
//            System.out.println(request.getParameter("userName"));

//            printTree(request);


//            org.springframework.security.web.servletapi.HttpServlet3RequestFactory$Servlet3SecurityContextHolderAwareRequestWrapper
//            getRemoteUser null
//            getUserPrincipal null
//            getMethod POST
//            getContextPath
//            getHeaderNames org.springframework.security.web.firewall.StrictHttpFirewall$StrictFirewalledRequest$2@22592773
//            getSession org.apache.catalina.session.StandardSessionFacade@7f457b3d
//            getRequestURI /user/login
//            getQueryString null
//            getRequestURL http://localhost:9000/user/login
//            getServletPath /user/login
//            getPathInfo null
//            getHttpServletMapping org.apache.catalina.core.ApplicationMapping$MappingImpl@2c9edd48
//            getCookies [Ljavax.servlet.http.Cookie;@28a14c03
//            getPathTranslated null
//            getAuthType null
//            getTrailerFields {}
//            getRequestedSessionId 33FF931C234C64C2CC0D023F2DD9F198
//            getScheme http
//            getProtocol HTTP/1.1
//            getInputStream org.apache.catalina.connector.CoyoteInputStream@53dac179
//            getContentLength 57
//            getContentLengthLong 57
//            getContentType application/json
//            getLocalPort 9000
//            getLocale zh_CN
//            getServletContext org.apache.catalina.core.ApplicationContextFacade@60080873
//            getParameterNames org.springframework.security.web.firewall.StrictHttpFirewall$StrictFirewalledRequest$3@6da3d773
//            getCharacterEncoding UTF-8
//            getDispatcherType REQUEST
//            getRequest org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterRequest@62a170b
//            getServerName localhost
//            getRemoteAddr 0:0:0:0:0:0:0:1
//            getRemoteHost 0:0:0:0:0:0:0:1
//            getServerPort 9000
//            getLocales java.util.Collections$3@5c04fe1b
//            getRemotePort 52272
//            getLocalName 0:0:0:0:0:0:0:1
//            getLocalAddr 0:0:0:0:0:0:0:1
//            getParameterMap org.apache.catalina.util.ParameterMap@6b5adb24
//            getAttributeNames java.util.Collections$3@7e6760cc
//            getClass class org.springframework.security.web.servletapi.HttpServlet3RequestFactory$Servlet3SecurityContextHolderAwareRequestWrapper

//  }
        return true;
    }

    public void printTree(Object object) throws InvocationTargetException, IllegalAccessException {
        Class<?> aClass = object.getClass();
        System.out.println(aClass.getName());
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            if (method.getName().startsWith("get")) {
                if (method.getName().equals("getAsyncContext")) continue;
                if (method.getName().equals("getParts")) continue;
                if (method.getName().equals("getReader")) continue;

//                System.out.println(method.getName());
                method.setAccessible(true);
                if (method.getGenericParameterTypes().length == 0) {
                    Object invoke = method.invoke(object);
                    System.out.println(method.getName()+" "+invoke);
                }

//                System.out.println(method.getName());
            }
        }
    }
}
